package Dictionary;

import com.sun.star.deployment.PackageInformationProvider;
import com.sun.star.deployment.XPackageInformationProvider;
import com.sun.star.uno.XComponentContext;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * This class contains the methods for accessing the data base and
 * creating necessary data structures        
 * @author SUMEDHA
 */
public class DataBaseReader {

    //To get necessary objects of Open office
    private final XComponentContext m_xContext;
    //To read the file
    private BufferedReader bufferedFileReader;
    //variables for store paths of the databases
    private String path, pathEngToSin, pathSinToEng,urlPath;
    //Hash maps are used as the datastructure to keep the dictionary data
    private HashMap<String, String> engToSin = new HashMap<String, String>();
    private HashMap<String, String> sinToEng = new HashMap<String, String>();
    //to keep words used in dictionary
    private String word, sinhalaWord, englishWord;
    //a variable to count no of words in the dictionary
    private int wordCounter;    

    /**
     * Creates a new DataBaseReader with the input argument
     * @param XComponentContext     Which is used to obtain the path to the Open Office working Directory
     */
    public DataBaseReader(XComponentContext context) {
        this.m_xContext = context;
    }

    /**
     * This method reads data from the database files. It uses Open office
     * Package information Provider to obtain the path where the extension is
     * saved. The necessary files are saved with the extension.
     *
     * @throws FileNotFoundException
     * @throws IOException
     */
    public void readData() throws FileNotFoundException, IOException {

        //Getting the location of the working place of the extension
        XPackageInformationProvider xPackageInformationProvider = PackageInformationProvider.get(m_xContext);
        String location = xPackageInformationProvider.getPackageLocation("com.example.SinhalaForOO");
        setPath(location);
        urlPath = getPath();
        //Operating systems path and Open Office path  are different, So generating OS path
        setPath(generatePath(getPath()));
        //To get data regarding variables
        Data data = new Data();         
        pathEngToSin = getPath() + data.getEngToSinLocation();
        pathSinToEng = getPath() + data.getSinToEngLocation();

        //Reading data for English To Sinhala dictionary to a HashMap
        bufferedFileReader = new BufferedReader(new InputStreamReader(new FileInputStream(pathEngToSin), "UTF8"));
        while ((word = bufferedFileReader.readLine()) != null) {
            wordCounter++;
            //Deviding the Word and the Meaning
            int indexOf = word.indexOf('=');
            englishWord = word.substring(0, indexOf);
            sinhalaWord = word.substring(indexOf + 1, word.length());
            //Inserting into the HashMap
            getEngToSin().put(englishWord, sinhalaWord);

        }

        //Reading data for sinhala dictionary
        bufferedFileReader = new BufferedReader(new InputStreamReader(new FileInputStream(pathSinToEng), "UTF8"));
        while ((word = bufferedFileReader.readLine()) != null) {
            wordCounter++;
            //Deviding the Word and the Meaning
            int indexOf = word.indexOf('=');
            sinhalaWord = word.substring(0, indexOf);
            englishWord = word.substring(indexOf + 1, word.length());
            //Inserting into the HashMap
            getSinToEng().put(sinhalaWord, englishWord);
        }
        //Closing the buffered reader
        bufferedFileReader.close();     
    }

    /**
     * Paths used in open office is different from the Windows path system.
     * So paths obtained through open office should be converted to Windows path system
     * This method performs that Operation.
     * @param path      The path to be converted
     * @return          The path which is converted to Windows path System
     */
    private String generatePath(String path) {
        //the forward slashes are replace by double back slashes to the path
        path = path.replace("/", "\\");
        //the first occurence of \\\ is removed
        path = path.replaceFirst("\\\\", "");
        //replace the space
        path = path.replace("%20", " ");         
        path = path.replace("file:\\\\", "");
        return path;

    }

    /**
     * @return the engToSin
     */
    public HashMap<String, String> getEngToSin() {
        return engToSin;
    }

    /**
     * @param engToSin the engToSin to set
     */
    public void setEngToSin(HashMap<String, String> engToSin) {
        this.setEngToSin(engToSin);
    }

    /**
     * @return the sinToEng
     */
    public HashMap<String, String> getSinToEng() {
        return sinToEng;
    }

    /**
     * @param sinToEng the sinToEng to set
     */
    public void setSinToEng(HashMap<String, String> sinToEng) {
        this.setSinToEng(sinToEng);
    }

    /**
     * @return the path
     */
    public String getPath() {
        return path;
    }

    /**
     * @param path the path to set
     */
    public void setPath(String path) {
        this.path = path;
    }

    /**
     * @return the urlPath
     */
    public String getUrlPath() {
        return urlPath;
    }
}
